package com.gzhryc.net_marketing.cloud.account.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.account.dao.CloudPayAccountWithdrawPayeeDao;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccount;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccountWithdrawPayee;
import com.gzhryc.net_marketing.cloud.account.services.dto.CloudPayAccountWithdrawPayeeSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CloudPayAccountWithdrawPayeeService {

    static Logger log = Logger.getLogger(CloudPayAccountWithdrawPayeeService.class);

    public static CloudPayAccountWithdrawPayeeService self() {
        return MultiDBTools.getService(MarketConstants.JDBC_KEY,CloudPayAccountWithdrawPayeeService.class, true);
    }

    CloudPayAccountWithdrawPayeeDao dao;

    public CloudPayAccountWithdrawPayeeService(String jdbcKey) {
        this.dao = new CloudPayAccountWithdrawPayeeDao(jdbcKey);
    }

    public List<CloudPayAccountWithdrawPayee> findJoinBySearch(CloudPayAccountWithdrawPayeeSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findJoinByCondition(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countJoinBySearch(CloudPayAccountWithdrawPayeeSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.countJoin(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<CloudPayAccountWithdrawPayee> findByAccountIdAndType(Long accountId, Integer type){
        if(accountId != null){
            Conditions conditions = new Conditions(CloudPayAccountWithdrawPayee.class);
            conditions.and().is("accountId",accountId);
            conditions.and().is("type",type);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public CloudPayAccountWithdrawPayee getByKeys(Long accountId, Integer type, String appId, String openId){
        if(accountId != null && type != null && StringTools.isNotBlank(appId) && StringTools.isNotBlank(openId)){
            Conditions conditions = new Conditions(CloudPayAccountWithdrawPayee.class);
            conditions.and().is("accountId",accountId);
            conditions.and().is("type",type);
            conditions.and().is("appId",appId);
            conditions.and().is("openId",openId);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(CloudPayAccountWithdrawPayee withdrawPayee) throws LogicException {
        CloudPayAccount account = CloudPayAccountService.self().getById(withdrawPayee.getAccountId());
        if(account == null){
            throw new LogicException("未找到云支付账号");
        }

        if(StringTools.isBlank(account.getWxpayAppId())){
            throw new LogicException("云支付账号未设置应用ID");
        }

        if(!account.getWxpayAppId().equals(withdrawPayee.getAppId())){
            throw new LogicException("云支付账号与取款应用ID不一致");
        }

        CloudPayAccountWithdrawPayee temp = getByKeys(withdrawPayee.getAccountId(),withdrawPayee.getType()
                ,withdrawPayee.getAppId(),withdrawPayee.getOpenId());
        if(temp != null){
            throw new LogicException("已绑定该"+temp.getTypeName()+"用户");
        }

        withdrawPayee.setCreateDate(new Date());
        try {
            return dao.insert(withdrawPayee) > 0;
        } catch (SQLException e) {
           log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
